<template>
  <div class="video-list-wrap">
    <div class="handle-wrap">
      <h4>视频列表</h4>
      <div>
        <el-button type="primary" @click="add">新增</el-button>
      </div>
    </div>
    <el-table :data="videoList" style="width: 100%">
      <el-table-column prop="title" label="标题"> </el-table-column>
      <el-table-column prop="content" label="内容"> </el-table-column>
      <el-table-column prop="user" label="作者" width="120">
        <template v-slot="scope">
          <span>{{ scope.row.user.userName }}</span>
        </template>
      </el-table-column>
      <el-table-column fixed="right" label="操作" width="250">
        <template v-slot="scope">
          <el-button
            @click.stop="editorRow(scope.row)"
            type="primary"
            size="small"
          >
            编辑
          </el-button>
          <el-button
            @click.stop="deleteRow(scope.row)"
            size="small"
          >
            删除
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <div class="pagination-container">
      <el-pagination
        background
        @current-change="currentChange"
        :page-size="pagination.pageSize"
        layout="prev, pager, next"
        :total="pagination.total"
      />
    </div>

    <el-dialog
      v-model="dialogFormVisible"
      :close-on-click-modal="false"
      :title="dialogTitle"
    >
      <el-form :model="form" label-position="top">
        <el-form-item label="标题" label-width="140px">
          <el-input v-model.trim="form.title" autocomplete="off" />
        </el-form-item>
        <el-form-item label="视频内容" label-width="140px">
          <el-input v-model.trim="form.content" autocomplete="off" />
        </el-form-item>
        <el-form-item label="缩略图" label-width="140px">
          <el-input v-model.trim="form.thumbnail" autocomplete="off" />
        </el-form-item>
        <el-form-item label="视频地址" label-width="140px">
          <el-input v-model.trim="form.video_url" autocomplete="off" />
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="dialogFormVisible = false">取消</el-button>
          <el-button type="primary" @click="confirm"> 确定 </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script>
import request from "utils/request";
import { ElMessage, ElMessageBox } from "element-plus";

export default {
  name: "JddChromePluginsVideoList",

  data() {
    return {
      videoList: [],
      dialogFormVisible: false,
      dialogTitle: "新增",
      form: {
        id: "",
        title: "",
        content: "",
        thumbnail: "",
        video_url: "",
      },
      pagination: {
        currentPage: 1,
        pageSize: 5,
        total: 0,
      },
    };
  },

  mounted() {
    this.getVideoList();
  },
  watch: {
    dialogFormVisible: {
      handler(value) {
        if (!value) {
          Object.assign(this.form, {
            id: "",
            title: "",
            content: "",
            thumbnail: "",
            video_url: "",
          });
          console.log("this.form", this.form);
        }
      },
    },
  },
  methods: {
    add() {
      this.dialogTitle = "新增";
      this.dialogFormVisible = true;
    },
    editorRow(video) {
      this.dialogTitle = "编辑";
      this.dialogFormVisible = true;
      Object.assign(this.form, video);
    },
    deleteRow(video) {
      ElMessageBox.confirm("确认需要删除该视频?", "警告", {
        confirmButtonText: "确认",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(async () => {
          let ret = await request.post("/deleteVideo", {
            id: video.id,
          });
          if (ret.code == 200) {
            this.$message({
              message: "移除成功",
              type: "success",
            });
            this.getVideoList();
          } else {
            this.$message({
              message: ret.message,
              type: "error",
            });
          }
        })
        .catch(() => {
          ElMessage({
            type: "info",
            message: "取消删除",
          });
        });
    },
    async confirm() {
      let ret = await request.post("/modifyVideo", {
        id: this.form.id,
        title: this.form.title,
        content: this.form.content,
        thumbnail: this.form.thumbnail,
        video_url: this.form.video_url,
      });

      if (ret.code == 200) {
        this.$message({
          message: "编辑成功",
          type: "success",
          showClose: true,
          duration: 1000,
        });
        this.getVideoList();
        this.dialogFormVisible = false;
      } else {
        this.$message({
          message: ret.message,
          type: "error",
          showClose: true,
          duration: 1000,
        });
      }
    },
    playVideo(video) {
      this.$router.push({
        path: "/video-player",
        query: {
          id: video.id,
        },
      });
    },
    async getVideoList() {
      let ret = await request.get("/videoList", {
        currentPage: this.pagination.currentPage,
        pageSize: this.pagination.pageSize,
      });
      if (ret.code == 200 && ret.data.videoList) {
        this.videoList = ret.data.videoList || [];
        this.pagination.total = ret.data.totalNum;
      }
    },
    async currentChange(pageSize) {
      this.pagination.currentPage = pageSize;
      this.getVideoList();
    },
  },
};
</script>

<style lang="scss" scoped>
.video-list-wrap {
  .handle-wrap {
    display: flex;
    justify-content: space-between;
    align-items: center;
  }
  .pagination-container {
    padding: 20px;
    display: flex;
    justify-content: flex-end;
  }
}
</style>